package net.hz.autohp;

import javax.swing.*;

import net.hz.utils.Constants.ButtonDetails;
import net.hz.utils.Constants.GatewayServers;
import net.hz.utils.Constants.IPDatabaseFilter;
import net.hz.utils.Constants.MissionType;
import net.hz.utils.HttpLoad;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
import java.util.TimerTask;
import java.util.Timer;

public class Gui {
	static Map<String, String> cookies;
	public static Timer timer = new Timer();
	final Gateway gw = new Gateway(cookies);
	
	final JLabel completeMissionTime = new JLabel(countdownTime(0));
	final JLabel findMissionTime = new JLabel(countdownTime(0));
	
	public static void main() {
		try {
			cookies = HttpLoad.login();
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		//new ShowProgress();
		
		new Gui();
	}
	
	public Gui() {
		//gw.crackServers();
		
		JFrame Frame1 = new JFrame("Auto HP");
		
		JPanel jp1 = new JPanel(new FlowLayout());
		JPanel jp2 = new JPanel();
		JPanel jp3 = new JPanel();
		
		JButton b1 = new JButton(ButtonDetails.PERFORM_EVENTS.text());
		
		JLabel l1 = new JLabel("Logged In - Session ID " + cookies.values());
		
		completeMissionTime.setFont(new Font("TimesRoman", Font.BOLD, 20));
		completeMissionTime.setForeground(Color.blue);
		findMissionTime.setFont(new Font("TimesRoman", Font.BOLD, 20));
		findMissionTime.setForeground(Color.red);
		
		// setup the buttons action listener
		b1.addActionListener(new actionListener());
		
		// add form items to the panels
		jp1.add(l1);
		jp2.add(completeMissionTime);
		jp2.add(b1);
		jp3.add(findMissionTime);
		
		// add panels to the frame
		Frame1.add(jp1);
		Frame1.add(jp2);
		Frame1.add(jp3);
		
		// setup the frame
		Frame1.setLayout(new FlowLayout());
		Frame1.setSize(400, 500);
		Frame1.setVisible(true);
		Frame1.setResizable(false);
		Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public String countdownTime(int value) {
		int hours = Math.round(value / (60 * 60));
		int minutes = Math.round(value / 60);
		int seconds = (value % 60);
		
		String currentTime = hours + ":" + checkTime(minutes) + ":"
                + checkTime(seconds);
		return currentTime;
	}
	
	public String currentTime() {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int aP = calendar.get(Calendar.AM_PM);
        String currentTime = hours + ":" + checkTime(minutes) + ":"
                + checkTime(seconds) + " " + amP(aP);
        return currentTime;
    }

    public String checkTime(int t) {
        String time1;
        if (t < 10) {
            time1 = ("0" + t);
        } else if (t == 60) {
            time1 = ("00");
        } else {
        	time1 = ("" + t);
        }
        
        return time1;
    }

    public String amP(int ap) {
        String amPm;
        if (ap == 0)
            amPm = "AM";
        else
            amPm = "PM";
        return amPm;
    }
    
    
    
	public class actionListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent ae) {
			gw.searchAndDestroy(GatewayServers.ONE);
			//gw.completeTasks(GatewayServers.ONE);
//			((JButton)ae.getSource()).removeActionListener(this);
//			if (ae.getActionCommand() == ButtonDetails.PERFORM_EVENTS.text()) {
//				timer.scheduleAtFixedRate(new performReoccuringEvents(), 0, 1000);
//			} 
		}
	}
	
	public class performReoccuringEvents extends TimerTask {
		int x = 0;
		int y = 0;
		
		@Override
		public void run() {
			try {
				completeMissionTime.setText(countdownTime(x--));
				findMissionTime.setText(countdownTime(y--));

				if (x <= 0) {
					gw.completeTasks(GatewayServers.ONE);
					x = 900;
				}
				
				if (y <= 0) {
					gw.findNewServers();
					gw.getMissions(IPDatabaseFilter.ALL, MissionType.DELETE);
					gw.performDeleteMissions();
					y = 1800;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
